package org.ycx.upms.web.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 杨 on 2017-06-12.
 */
@Configuration
public class DruidDataSourceConfig {

    @Value("${master.datasource.url}")
    private String masterUrl;
    @Value("${master.datasource.username}")
    private String masterUser;
    @Value("${master.datasource.password}")
    private String masterPassword;

    @Value("${cluster.datasource.url}")
    private String slaveUrl;

    @Value("${cluster.datasource.username}")
    private String slaveUser;

    @Value("${cluster.datasource.password}")
    private String slavePassword;

    @Value("${datasource.driveClassName}")
    private String driverClass;

    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(masterUrl);
        dataSource.setUsername(masterUser);
        dataSource.setPassword(masterPassword);
        return dataSource;
    }

    public DataSource slaveDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(slaveUrl);
        dataSource.setUsername(slaveUser);
        dataSource.setPassword(slavePassword);
        return dataSource;
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager (masterDataSource());
    }
    @Bean(name = "clusterTransactionManager")
    public DataSourceTransactionManager clusterTransactionManager() {
        return new DataSourceTransactionManager (masterDataSource());
    }

    @Bean("dynamicDataSource")
    public DataSource dynamicDataSource(){
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
        Map<Object,Object> map =new HashMap<Object,Object>();
        map.put(DataSourceEnum.MASTER,masterDataSource());
        map.put(DataSourceEnum.SLAVE,slaveDataSource());
        dynamicDataSource.setTargetDataSources(map);
        return dynamicDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactoryBean(){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean ();
        bean.setDataSource(dynamicDataSource());
        bean.setConfigLocation(new ClassPathResource ("mybatis-config.xml"));
        return bean;
    }
}
